package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.BatInstBatchDao;
import com.fdb.efp.nls.service.domain.BatInstBatch;
import com.fdb.efp.nls.service.facade.BatInstBatchService;
import com.fdb.efp.nls.service.vo.BatInstBatchVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 批次实例信息
 * <p>
 * 
 * @author ligm
 * @date 2018-06-13
 */
@Service("batInstBatchService")
public class BatInstBatchServiceImpl implements BatInstBatchService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BatInstBatchServiceImpl.class);

	@Autowired
	private BatInstBatchDao batInstBatchDao;

	/**
	 * 新增操作
	 */
	public int insertBatInstBatch(BatInstBatchVO inBatInstBatchVo) {
		logger.debug("当前新增数据为:{}", inBatInstBatchVo.toString());
		int num = 0;
		try {
			BatInstBatch batInstBatch = new BatInstBatch();
			beanCopy(inBatInstBatchVo, batInstBatch);
			num = batInstBatchDao.insertBatInstBatch(batInstBatch);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:{}", num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BatInstBatchVO inBatInstBatchVo) {
		logger.debug("当前删除数据条件为:{}", inBatInstBatchVo);
		int num = 0;
		try {
			BatInstBatch batInstBatch = new BatInstBatch();
			beanCopy(inBatInstBatchVo, batInstBatch);
			num = batInstBatchDao.deleteByPk(batInstBatch);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:{}删除的数据条数为{}", inBatInstBatchVo, num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(BatInstBatchVO inBatInstBatchVo) {
		logger.debug("当前修改数据为:{}", inBatInstBatchVo.toString());
		int num = 0;
		try {
			BatInstBatch batInstBatch = new BatInstBatch();
			beanCopy(inBatInstBatchVo, batInstBatch);
			num = batInstBatchDao.updateByPk(batInstBatch);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:{}修改的数据条数为{}", inBatInstBatchVo, num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public BatInstBatchVO queryByPk(BatInstBatchVO inBatInstBatchVo) {
		logger.debug("当前查询参数信息为:{}", inBatInstBatchVo);
		try {
			BatInstBatch queryBatInstBatch = new BatInstBatch();
			beanCopy(inBatInstBatchVo, queryBatInstBatch);
			BatInstBatch queryRslBatInstBatch = batInstBatchDao.queryByPk(queryBatInstBatch);
			if (Objects.nonNull(queryRslBatInstBatch)) {
				BatInstBatchVO outBatInstBatchVo = beanCopy(queryRslBatInstBatch, new BatInstBatchVO());
				logger.debug("当前查询结果为:{}", outBatInstBatchVo.toString());
				return outBatInstBatchVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public int updateBatInstBatchStatus(BatInstBatchVO batInstBatchVo) {
		logger.debug("当前修改状态数据为:{}", batInstBatchVo.toString());
		int num = 0;
		try {
			BatInstBatch batInstBatch = new BatInstBatch();
			beanCopy(batInstBatchVo, batInstBatch);
			num = batInstBatchDao.updateBatInstBatchStatus(batInstBatch);
		} catch (Exception e) {
			logger.error("修改状态数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:{}修改的状态数据条数为{}", batInstBatchVo, num);
		return num;
	}

	@Override
	public BatInstBatchVO queryByCondition(BatInstBatchVO batInstBatchVo) {
		logger.debug("当前查询参数信息为:{}", batInstBatchVo);
		try {
			BatInstBatch queryBatInstBatch = new BatInstBatch();
			beanCopy(batInstBatchVo, queryBatInstBatch);
			BatInstBatch queryRslBatInstBatch = batInstBatchDao.queryByCondition(queryBatInstBatch);
			if (Objects.nonNull(queryRslBatInstBatch)) {
				BatInstBatchVO outBatInstBatchVo = beanCopy(queryRslBatInstBatch, new BatInstBatchVO());
				logger.debug("当前查询结果为:{}", outBatInstBatchVo.toString());
				return outBatInstBatchVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 按条件查询批次集合
	 *
	 * @author wanglaisheng
	 * @date 20201126
	 * @param queryCondition
	 * @return list 批次集合
	 */
	@Override
	public List<BatInstBatchVO> queryListByCondition(BatInstBatchVO queryCondition) {
		logger.debug("当前查询参数信息为:{}", queryCondition);
		try {
			BatInstBatch queryBatInstBatch = new BatInstBatch();
			beanCopy(queryCondition, queryBatInstBatch);
			List<BatInstBatch> lstBatInstBatch = batInstBatchDao.queryListByCondition(queryBatInstBatch);
			if (CollectionUtils.isNotEmpty(lstBatInstBatch)) {
				List<BatInstBatchVO> lstBatInstBatchVO = new ArrayList<>();
				for (BatInstBatch batInstBatch : lstBatInstBatch) {
					BatInstBatchVO batInstBatchVo = beanCopy(batInstBatch, new BatInstBatchVO());
					lstBatInstBatchVO.add(batInstBatchVo);
				}
				logger.debug("当前查询结果为:{}", lstBatInstBatch.toString());
				return lstBatInstBatchVO;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
}
